// mircoApp/new_coupon/new_CouponCombinationDetail/new_CouponCombinationDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    member_coupon_ids: [],
    arrayKey: 0,
    result: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toBackRelpace() {
    wx.navigateBack({
      delta: '1',
      fail:function(){
        console.log('wwwwwwwwwwwwwww')
        wx.redirectTo({
          url: '/mircoApp/new_coupon/new_coupon/new_coupon',
        })
      }
    })
  },
  tapShowAll(e) {
    let {
      ind
    } = e.currentTarget.dataset;
    this.setData({
      ['info.combination_detail[' + ind + '].hidden']: true
    })
  },
  btnPosCheck_m() {
    if (!this.data.info) return;
    let check = true;
    let combination_detail = this.data.info?.combination_detail;
    for (let i = 0; i < combination_detail.length; i++) {
      if (combination_detail[i].num > combination_detail[i].member_coupon_ids.length) {
        check = false;
      }
    }
    return check;
  },
  async tapCombination() {
    if (!this.btnPosCheck_m()) return app.tips('请选择组合所需优惠券');
    let combination_detail = this.data.info.combination_detail;
    let arr = [];
    for (let i = 0; i < combination_detail.length; i++) {
      arr.push(...combination_detail[i].member_coupon_ids);
    }
    let json = {
      id: this.options.id,
      member_coupon_ids: arr
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.combination.combination"),
      data: json
    })
    if (data.result) {
      app.tips(data.msg);
      wx.redirectTo({
        url:'/mircoApp/new_coupon/new_coupon_detail/new_coupon_detail?name=new_coupon_detail_combination&id=' + this.data.info.combination_coupon.coupon_id,
      })
    } else {
      app.tips(data.msg);
    }
  },
  checkOnChange(e) {
    let ind = e.currentTarget.dataset.ind;
    let detail = e.detail;

    this.setData({
      ['info.combination_detail[' + ind + '].member_coupon_ids']: detail
    })
    let combination = this.data.info.combination_detail[ind]
    console.log(combination, 'combination')
    if (combination.member_coupon_ids.length < combination.num || combination.member_coupon_ids.length == 0) {
      combination.member_coupon.forEach((ele, index) => {
        ele.hide = false;
      });
      console.log(combination.member_coupon, 'combination.member_coupon')
      this.setData({
        ['info.combination_detail[' + ind + '].member_coupon']: combination.member_coupon
      })
    } else if (combination.member_coupon_ids.length == combination.num) {
      console.log(combination.member_coupon)
      for (let i = 0; i < combination.member_coupon.length; i++) {
        combination.member_coupon[i].hide = true;
        for (let k = 0; k < combination.member_coupon_ids.length; k++) {
          if (combination.member_coupon_ids[k] == combination.member_coupon[i].id) {
            console.log(combination.member_coupon[i], 'wwwwwwwwwqqqqqqqq')
            combination.member_coupon[i].hide = false;
          }
        }
      }
      console.log(combination.member_coupon)
      // var inter = combination.member_coupon.filter((ele) => combination.member_coupon_ids.indexOf(String(ele.id)))
      // inter.forEach((ele, index) => {
      //   ele.hide = true;
      // })
      // if (inter.length == 0) return
      this.setData({
        ['info.combination_detail[' + ind + '].member_coupon']: combination.member_coupon
      })
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.combination.detail"),
      data: {
        id: this.options.id
      }
    })
    if (data.result) {
      this.setData({
        info: data.data.info,
      })
      this.defaultSelection();
    } else {
      app.tips(data.msg);
    }
  },
  defaultSelection() {
    let array = this.data.info.combination_detail;
    for (let i = 0; i < array.length; i++) {
      array[i].member_coupon_ids = new Array();
      for (let k = 0; k < array[i].member_coupon.length; k++) {
        if (k < array[i].num) {
          array[i].member_coupon_ids.push(String(array[i].member_coupon[k].id));
        }
        if (k >= array[i].num) {
          array[i].member_coupon[k].hide = true;
        }
      }
    }
    this.setData({
      info: this.data.info
    })
  }
})